home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / progs / examples / movelight.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  8KB  |  288 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. /**
  9.  * (c) Copyright 1993, Silicon Graphics, Inc.
  10.  * ALL RIGHTS RESERVED 
  11.  * Permission to use, copy, modify, and distribute this software for 
  12.  * any purpose and without fee is hereby granted, provided that the above
  13.  * copyright notice appear in all copies and that both the copyright notice
  14.  * and this permission notice appear in supporting documentation, and that 
  15.  * the name of Silicon Graphics, Inc. not be used in advertising
  16.  * or publicity pertaining to distribution of the software without specific,
  17.  * written prior permission. 
  18.  *
  19.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  20.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  21.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  22.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  23.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  24.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  25.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  26.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  27.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  28.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  29.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  30.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  31.  * 
  32.  * US Government Users Restricted Rights 
  33.  * Use, duplication, or disclosure by the Government is subject to
  34.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  35.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  36.  * clause at DFARS 252.227-7013 and/or in similar or successor
  37.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  38.  * Unpublished-- rights reserved under the copyright laws of the
  39.  * United States.  Contractor/manufacturer is Silicon Graphics,
  40.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  41.  *
  42.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  43.  */
  44. /**
  45.  *  movelight.c
  46.  *  This program demonstrates when to issue lighting and 
  47.  *  transformation commands to render a model with a light 
  48.  *  which is moved by a modeling transformation (rotate or 
  49.  *  translate).  The light position is reset after the modeling 
  50.  *  transformation is called.  The eye position does not change.
  51.  *
  52.  *  A sphere is drawn using a grey material characteristic. 
  53.  *  A single light source illuminates the object.
  54.  *
  55.  *  Interaction:  pressing the left or middle mouse button
  56.  *  alters the modeling transformation (x rotation) by 30 degrees.  
  57.  *  The scene is then redrawn with the light in a new position.
  58.  */
  59. #include <stdlib.h>
  60. #include <stdarg.h>
  61. #include <stdio.h>
  62. #include <GL/glut.h>
  63.  
  64. #define TORUS 0
  65. #define TEAPOT 1
  66. #define DOD 2
  67. #define TET 3
  68. #define ISO 4
  69. #define QUIT 5
  70.  
  71. static int spin = 0;
  72. static int obj = TORUS;
  73. static int begin;
  74.  
  75. void 
  76. output(GLfloat x, GLfloat y, char *format,...)
  77. {
  78.   va_list args;
  79.   char buffer[200], *p;
  80.  
  81.   va_start(args, format);
  82.   vsprintf(buffer, format, args);
  83.   va_end(args);
  84.   glPushMatrix();
  85.   glTranslatef(x, y, 0);
  86.   for (p = buffer; *p; p++)
  87.     glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
  88.   glPopMatrix();
  89. }
  90.  
  91. void
  92. menu_select(int item)
  93. {
  94.   if (item == QUIT)
  95.     exit(0);
  96.   obj = item;
  97.   glutPostRedisplay();
  98. }
  99.  
  100. void
  101. movelight(int button, int state, int x, int y)
  102. {
  103.   if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
  104.     begin = x;
  105.   }
  106. }
  107.  
  108. void
  109. motion(int x, int y)
  110. {
  111.   spin = (spin + (x - begin)) % 360;
  112.   begin = x;
  113.   glutPostRedisplay();
  114. }
  115.  
  116. void
  117. myinit(void)
  118. {
  119.   glEnable(GL_LIGHTING);
  120.   glEnable(GL_LIGHT0);
  121.  
  122.   glDepthFunc(GL_LESS);
  123.   glEnable(GL_DEPTH_TEST);
  124. }
  125.  
  126. /*  Here is where the light position is reset after the modeling
  127.  *  transformation (glRotated) is called.  This places the 
  128.  *  light at a new position in world coordinates.  The cube
  129.  *  represents the position of the light.
  130.  */
  131. void
  132. display(void)
  133. {
  134.   GLfloat position[] =
  135.   {0.0, 0.0, 1.5, 1.0};
  136.  
  137.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  138.   glMatrixMode(GL_MODELVIEW);
  139.   glPushMatrix();
  140.   glTranslatef(0.0, 0.0, -5.0);
  141.  
  142.   glPushMatrix();
  143.   glRotated((GLdouble) spin, 0.0, 1.0, 0.0);
  144.   glRotated(0.0, 1.0, 0.0, 0.0);
  145.   glLightfv(GL_LIGHT0, GL_POSITION, position);
  146.  
  147.   glTranslated(0.0, 0.0, 1.5);
  148.   glDisable(GL_LIGHTING);
  149.   glColor3f(0.0, 1.0, 1.0);
  150.   glutWireCube(0.1);
  151.   glEnable(GL_LIGHTING);
  152.   glPopMatrix();
  153.  
  154.   switch (obj) {
  155.   case TORUS:
  156.     glutSolidTorus(0.275, 0.85, 20, 20);
  157.     break;
  158.   case TEAPOT:
  159.     glutSolidTeapot(1.0);
  160.     break;
  161.   case DOD:
  162.     glPushMatrix();
  163.     glScalef(.5, .5, .5);
  164.     glutSolidDodecahedron();
  165.     glPopMatrix();
  166.     break;
  167.   case TET:
  168.     glutSolidTetrahedron();
  169.     break;
  170.   case ISO:
  171.     glutSolidIcosahedron();
  172.     break;
  173.   }
  174.  
  175.   glPopMatrix();
  176.   glPushAttrib(GL_ENABLE_BIT);
  177.   glDisable(GL_DEPTH_TEST);
  178.   glDisable(GL_LIGHTING);
  179.   glMatrixMode(GL_PROJECTION);
  180.   glPushMatrix();
  181.   glLoadIdentity();
  182.   gluOrtho2D(0, 3000, 0, 3000);
  183.   glMatrixMode(GL_MODELVIEW);
  184.   glPushMatrix();
  185.   glLoadIdentity();
  186.   output(80, 2800, "Welcome to movelight.");
  187.   output(80, 2650, "Right mouse button for menu.");
  188.   output(80, 400, "Hold down the left mouse button");
  189.   output(80, 250, "and move the mouse horizontally");
  190.   output(80, 100, "to change the light position.");
  191.   glPopMatrix();
  192.   glMatrixMode(GL_PROJECTION);
  193.   glPopMatrix();
  194.   glPopAttrib();
  195.   glutSwapBuffers();
  196. }
  197.  
  198. void
  199. myReshape(int w, int h)
  200. {
  201.   glViewport(0, 0, w, h);
  202.   glMatrixMode(GL_PROJECTION);
  203.   glLoadIdentity();
  204.   gluPerspective(40.0, (GLfloat) w / (GLfloat) h, 1.0, 20.0);
  205.   glMatrixMode(GL_MODELVIEW);
  206. }
  207.  
  208. void
  209. tmotion(int x, int y)
  210. {
  211.   printf("Tablet motion x = %d, y = %d\n", x, y);
  212. }
  213.  
  214. void
  215. tbutton(int b, int s, int x, int y)
  216. {
  217.   printf("b = %d, s = %d, x = %d, y = %d\n", b, s, x, y);
  218. }
  219.  
  220. void
  221. smotion(int x, int y, int z)
  222. {
  223.    fprintf(stderr, "Spaceball motion %d %d %d\n", x, y, z);
  224. }
  225.  
  226. void
  227. rmotion(int x, int y, int z)
  228. {
  229.    fprintf(stderr, "Spaceball rotate %d %d %d\n", x, y, z);
  230. }
  231.  
  232. void
  233. sbutton(int button, int state)
  234. {
  235.   fprintf(stderr, "Spaceball button %d is %s\n",
  236.     button, state == GLUT_UP ? "up" : "down");
  237. }
  238.  
  239. void
  240. dials(int dial, int value)
  241. {
  242.   fprintf(stderr, "Dial %d is %d\n", dial, value);
  243.   spin = value % 360;
  244.   glutPostRedisplay();
  245. }
  246.  
  247. void
  248. buttons(int button, int state)
  249. {
  250.   fprintf(stderr, "Button %d is %s\n", button,
  251.     state == GLUT_UP ? "up" : "down");
  252. }
  253.  
  254. /*  Main Loop
  255.  *  Open window with initial window size, title bar, 
  256.  *  RGBA display mode, and handle input events.
  257.  */
  258. int
  259. main(int argc, char **argv)
  260. {
  261.   glutInit(&argc, argv);
  262.   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  263.   glutInitWindowSize(500, 500);
  264.   glutCreateWindow(argv[0]);
  265.   myinit();
  266.   glutMouseFunc(movelight);
  267.   glutMotionFunc(motion);
  268.   glutReshapeFunc(myReshape);
  269.   glutDisplayFunc(display);
  270.   glutTabletMotionFunc(tmotion);
  271.   glutTabletButtonFunc(tbutton);
  272.   glutSpaceballMotionFunc(smotion);
  273.   glutSpaceballRotateFunc(rmotion);
  274.   glutSpaceballButtonFunc(sbutton);
  275.   glutDialsFunc(dials);
  276.   glutButtonBoxFunc(buttons);
  277.   glutCreateMenu(menu_select);
  278.   glutAddMenuEntry("Torus", TORUS);
  279.   glutAddMenuEntry("Teapot", TEAPOT);
  280.   glutAddMenuEntry("Dodecahedron", DOD);
  281.   glutAddMenuEntry("Tetrahedron", TET);
  282.   glutAddMenuEntry("Icosahedron", ISO);
  283.   glutAddMenuEntry("Quit", QUIT);
  284.   glutAttachMenu(GLUT_RIGHT_BUTTON);
  285.   glutMainLoop();
  286.   return 0;             /* ANSI C requires main to return int. */
  287. }
  288.